﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Admin_AddnewEmployee : System.Web.UI.Page
{
    AddminMethord AM = new AddminMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtBirthOfDate_CalendarExtender.EndDate = DateTime.Now;
        LoadUserName();
    }
    private void LoadUserName()
    {
        if (ddlType.SelectedIndex == 0)
        {
            txtUserName.Text = AM.HRIDNew();
        }
        else
        {
            txtUserName.Text = AM.InterviewerIDNew();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblErors.Text = "";
        lblSuccess.Text = "";
        string mathEmail = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        string mathPhone = @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$";
        if (txtFullName.Text == "")
        {
            lblErors.Text += "Please Enter FullName";
        }
        else if (txtAddress.Text == "")
        {
            lblErors.Text += "Please Enter Address";
        }
        else if ((txtPhone.Text == "")||(Regex.IsMatch(txtPhone.Text, mathPhone))==false)
        {
            lblErors.Text += "Please Enter Phone";
        }
        else if (txtBirthOfDate.Text == "")
        {
            lblErors.Text += "Please Enter BirthOfDate";
        }
        else if ((txtEmail.Text == "") || (Regex.IsMatch(txtEmail.Text, mathEmail)) == false)
        {
            lblErors.Text += "Please Enter Email";
        }
        else
        {
            try
            {
                Convert.ToDateTime(txtBirthOfDate.Text);
                if (ddlType.SelectedItem.ToString().Equals("HR"))
                {
                    AddnewHR();
                }
                else
                {
                    AddnewInteviewer();
                }
            }
            catch (FormatException fx)
            {
                lblErors.Text = "Check Date of birth.";
            }
            catch (Exception ex)
            {
            }
        }
        
    }
    public void AddnewHR() {
        try
        {
            DateTime newdate ;
            newdate = Convert.ToDateTime(txtBirthOfDate.Text);
            
            bool check = AM.AddnewHR(txtUserName.Text, txtFullName.Text, txtAddress.Text, txtPhone.Text,newdate, txtEmail.Text);
            if (check == true)
            {
                lblSuccess.Text = "Add new HR Successfull";
                txtFullName.Enabled = false;
                txtAddress.Enabled = false;
                txtPhone.Enabled = false;
                txtBirthOfDate.Enabled = false;
                txtEmail.Enabled = false;
            }
            else {
                lblErors.Text = "Add new HR UnSuccess";
            }
        }
        catch (Exception ex)
        {
        } 
    }
    public void AddnewInteviewer() 
    {
        try
        {
            DateTime newdate;
            newdate = Convert.ToDateTime(txtBirthOfDate.Text);

            bool check = AM.AddnewInterviewer(txtUserName.Text, txtFullName.Text, txtAddress.Text, txtPhone.Text, newdate, txtEmail.Text);
            if (check == true)
            {
                lblSuccess.Text = "Add new Interviewer Successfull";
                txtFullName.Enabled = false;
                txtAddress.Enabled = false;
                txtPhone.Enabled = false;
                txtBirthOfDate.Enabled = false;
                txtEmail.Enabled = false;
            }
            else
            {
                lblErors.Text = "Add new Interviewer UnSuccess";
            }
        }
        catch (Exception ex)
        {
        } 
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/AddnewEmployee.aspx");
    }
}